﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyFrmClock
{
    public partial class UcClock : UserControl
    {
        public UcClock()
        {
            InitializeComponent();
            //设置缓冲，防止表盘闪烁
            DoubleBuffered= true;
        }
        /// <summary>
        /// 绘制钟表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UcClock_Paint(object sender, PaintEventArgs e)
        {
            Graphics g=e.Graphics;
            Pen pen = new Pen(new SolidBrush(Color.Black),4);
            Brush brush = new SolidBrush(Color.LightSkyBlue);
            Brush hourbrush=new SolidBrush(Color.Black);
            Brush minbrush = new SolidBrush(Color.Black);
            Brush secbrush=new SolidBrush(Color.Brown);
            Pen hourpen=new Pen(hourbrush,4);
            Pen minpen=new Pen(minbrush,2);
            Pen secpen=new Pen(secbrush,1);
            int width = 200;
            //找到中心点
            int centrewid=this.Width/2;
            int centrehei=this.Height/2;
            Rectangle rec = new Rectangle(centrewid-width/2, centrehei-width/2, width, width);
            //绘制表盘
            g.FillEllipse(brush, rec);
            g.DrawEllipse(pen, rec);
            //设置数字样式
            g.TranslateTransform(centrewid, centrehei);
            g.FillEllipse(hourbrush,new RectangleF(-2,-2,4,4));
            Point start=new Point(0,0);
            PointF numend = new PointF(0, -width * 15 / 32);
            Font font = new Font("宋体", 12,FontStyle.Bold);
            //绘制表盘数字
            for(int i=1;i<=12;i++)
            {
                if (i < 10)
                {
                    g.RotateTransform(i * 360 / 12 - 5);
                    g.DrawString(i.ToString(), font, hourbrush, numend);
                    g.RotateTransform(-i * 360 / 12 + 5);
                }
                else
                {
                    g.RotateTransform(i * 360 / 12 - 8);
                    g.DrawString(i.ToString(), font, hourbrush, numend);
                    g.RotateTransform(-i * 360 / 12 + 8);
                }
            }
            //得到三根针长度
            Point secend = new Point(0, -width * 7/16);
            Point minend=new Point(0, -width*3/8);
            Point hourend = new Point(0, -width * 5 / 16);
            //确定现在时间
            int sec = DateTime.Now.Second;
            int min = DateTime.Now.Minute;
            int hour=DateTime.Now.Hour;
            //先绘制秒针
            g.RotateTransform(sec*360/60);
            g.DrawLine(secpen, start, secend);
            g.RotateTransform(-sec * 360 / 60);
            //再绘制分针
            g.RotateTransform(min * 360 / 60);
            g.DrawLine(minpen, start, minend);
            g.RotateTransform(-min * 360 / 60);
            //最后绘制时针
            g.RotateTransform(hour * 360 / 12);
            g.DrawLine(hourpen, start, hourend);
            g.RotateTransform(-hour * 360 / 12);
        }
        /// <summary>
        /// 重新绘制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}
